
package com.pricl.wechat.faq.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.pricl.wechat.faq.dao.FaqCommentDao;
import com.pricl.wechat.faq.dao.FaqQuestionDao;
import com.pricl.wechat.faq.dao.FaqUserDao;
import com.pricl.wechat.faq.entity.FaqUser;
import com.pricl.wechat.faq.module.FaqCommentModule;
import com.pricl.wechat.faq.module.FaqQuestionModule;
import com.pricl.wechat.faq.page.FaqCommentPage;
import com.pricl.wechat.faq.page.FaqQuestionPage;
import com.pricl.wechat.faq.service.FaqCommentService;
import com.pricl.wechat.faq.service.FaqQuestionService;
import com.pricl.wechat.faq.service.FaqUserService;

/**
 * 问答页面的主要逻辑。主要用于获取值 
 * @author winton
 *
 */
@Controller
@RequestMapping(value = "/wechat/faq")
public class IndexController{
	
	@Autowired
	FaqQuestionService faqQuestionService;
	
	@Autowired
	FaqQuestionDao faqQuestionDao;
	
	/**
	 * 评论dao
	 */
	@Autowired
	FaqCommentDao faqCommentDao;
	
	/**
	 * 评论service
	 */
	@Autowired
	FaqCommentService faqCommentService;
	
	/**
	 * 用户service
	 */
	@Autowired
	FaqUserService faqUserService;
	
	/**
	 * 用户dao
	 */
	@Autowired
	FaqUserDao faqUserDao;
	
	/**
	 * 主页
	 * @return
	 */
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(HttpServletRequest req) {
		return "wechat_faq/index";
	}

	/**
	 * 问题详情
	 * @return
	 */
	@RequestMapping(value = "detail", method = RequestMethod.GET)
	public String detail(Model model, String id, FaqCommentPage page) {
		Map<String,String> parameters = Maps.newHashMap();
		parameters.put("questionId", id);
		parameters.put("enable", "0");
		parameters.put("order", " topComment DESC,countLike DESC,createTime DESC ");
		
		FaqQuestionModule faqQuestion = new FaqQuestionModule();
		List<FaqQuestionModule> selectQuestionListReleationInfo = faqQuestionDao.selectQuestionListReleationInfo(Long.valueOf(id));
		if (selectQuestionListReleationInfo == null || selectQuestionListReleationInfo.size() == 0) {
			faqQuestion.setQuesttionContent("该问题不存在或者已删除");
		} else {
			faqQuestion = selectQuestionListReleationInfo.get(0);
			List<FaqCommentModule> faqcommentList = faqCommentDao.getCommentInfoByQuestionId(parameters);
			model.addAttribute("commentList", faqcommentList);
		}
		
		model.addAttribute("question", faqQuestion);
		return "wechat_faq/detail";
	}
	
	/**
	 * 个人中心
	 * @return
	 */ 
	@RequestMapping(value = "usercenter", method = RequestMethod.GET)
	public String userCenter(HttpServletRequest req, Model model) {
		// 获取到用户信息，并查找相关的头像昵称，发出的问题等。
		long userId = (long) req.getSession().getAttribute("id");
		FaqUser faqUser = faqUserService.selectByPrimaryKey(userId);
		model.addAttribute("userinfo", faqUser);
		return "wechat_faq/user_center";
	}
	
	/**
	 * 异步请求个人中心我的动态
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "myNews", method = RequestMethod.GET)
	public Map<String,Object> myNews(HttpServletRequest req, HttpServletResponse resp, Model model, FaqCommentPage page) {
		// 获取发出的问题
		// 查找该用户的问题和该问题的最新评论
		// 进行问题和热评的长度截取
		
		// 获取到用户id
		long userId = (long) req.getSession().getAttribute("id");

		page.setQuestionUserId(userId);
		page.setOrder(" createTime DESC ");
		page.setCommentStatus(0);
		page.setQuestionStatus(0);
		faqCommentService.page(page);
		
		Map<String,Object> resultMap = Maps.newHashMap();
		resultMap.put("centerList", page);
		return resultMap;
	}
	
	/**
	 * 异步请求个人中心我的问题
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "myQuestions", method = RequestMethod.GET)
	public Map<String,Object> myQuestions(HttpServletRequest req, HttpServletResponse resp, Model model, FaqQuestionPage page) {
		// 获取发出的问题
		// 查找该用户的问题和该问题的最新评论
		// 进行问题和热评的长度截取
		
		// 获取到用户id
		long userId = (long) req.getSession().getAttribute("id");
		
		page.setUserId(userId);
		page.setHasAudit(0);
		page.setOrder(" createTime DESC ");
		faqQuestionService.page(page);
		
		Map<String,Object> resultMap = Maps.newHashMap();
		resultMap.put("centerList", page);
		return resultMap;
	}
	
	/**
	 * 异步主页问题列表请求
	 * @param orderFlag 1按照数据排序， 0按照评论数
	 */
	@ResponseBody
	@RequestMapping(value = "questionlist", method = RequestMethod.GET)
	public Map<String,Object> questionlist(Model model, FaqQuestionPage page, String orderFlag) {
		// 构建查询条件
		if ("0".equals(orderFlag)) {
			page.setOrder(" commentCount DESC");
		} else { // 如果没有则默认为根据最多评论数算,并给orderFlag默认为0
			page.setOrder(" createTime DESC");
		}
		page.setHasAudit(0);
		page = faqQuestionService.page(page);
		
		Map<String,Object> resultMap = Maps.newHashMap();
		resultMap.put("questionList", page);
		resultMap.put("orderFlag", orderFlag);
		return resultMap;
	}
}
